<?php
/* @var $this ColorController */
/* @var $model Color */
$this->pageTitle = Yii::app()->name . ' - '.Yii::t('default', 'Category');
$this->breadcrumbs=array(
Yii::t('default', 'Category')=>array('index'),
Yii::t('default', 'List'),
);

$this->menu=array(
  array('label'=>Yii::t('default', 'Create'), 'url'=>array('create')),
);
?>

<h3>
<?php echo Yii::t('default', 'List').' '.Yii::t('default', 'Category') ?>
</h3>

<?php if (Yii::app()->user->hasFlash('success')) {?>
  <div class="alert alert-success">
    <?php echo Yii::app()->user->getFlash('success')?>
  </div>
<?php }?>
<?php if (Yii::app()->user->hasFlash('notice')) {?>
  <div class="alert">
    <?php echo Yii::app()->user->getFlash('notice')?>
  </div>
<?php }?>

<?php
$pageSize = Yii::app()->user->getState('pageSize', 10);

$this->widget('zii.widgets.grid.CGridView', array(
  'id' => 'category-grid',
  'dataProvider' => $model->search(),
  'rowCssClassExpression' => '"items[]_{$data->id}"',
  'afterAjaxUpdate' => 'installSortable',
  'pager' => array(
    'firstPageLabel' => '&lt;&lt;',
    'prevPageLabel' => '&lt;',
    'nextPageLabel' => '&gt;',
    'lastPageLabel' => '&gt;&gt;',
    'footer' => '',
    'header' => '',
    'selectedPageCssClass' => 'selected',
    'firstPageCssClass' => '',
    'lastPageCssClass' => '',
    'nextPageCssClass' => '',
    'previousPageCssClass' => '',
    'internalPageCssClass' => '',
    'htmlOptions' => array(
      'class' => ''
    )
  ),
  'pagerCssClass' => 'pagination',
  'columns' => array(
    array(
      'value' => "getNoOrder($this->stt_gridview)",
      'type' => 'raw'
    ),
    array(
      'name' => 'image',
      'value' => '$data->getCategoryImage($data->id, 90, 85)',
      'type' => 'raw'
    ),
    array(
      'name' => 'name',
      'value' => '$data->name'
    ),
    array(
      'name' => 'enabled',
      'value' => '($data->enabled==1)?Yii::t("default", "Yes"):Yii::t("default", "No")'
    ),
    array(
      'name' => 'createddate',
      'value' => 'date("d-m-Y G:i",$data->createddate)'
    ),
    array(
      'name' => 'modifieddate',
      'value' => 'date("d-m-Y G:i",$data->modifieddate)'
    ),
    array(
      'class' => 'CButtonColumn',
      'buttons'=>array(
  			'delete'=>array('url'=>'"javascript:validateDelete($data->id)"','click'=>'""'),
  		),
    )
  ),
  'htmlOptions' => array(
    'class' => ''
  )
));
?>
<div class="row_per_page">
<?php 
  $all = $model->count();
  echo Yii::t('default', 'Row per page: ').CHtml::dropDownList('pageSize',$pageSize,array(10=>10,20=>20,50=>50,$all=>Yii::t('default', 'All')),array(
        'onchange'=>"$.fn.yiiGridView.update('category-grid',{ data:{pageSize: $(this).val() }})",
    ))
?>
</div>
<script type="text/javascript">
function validateDelete(id){
  var notice = '<?php echo Yii::t('default', 'Are you sure you want to delete this item?');?>';
  if(confirm(notice)){
    $.ajax({
        "url":baseUrl+"/admin/category/delete",
        data:{id: id, action: 'index'},
        type:'get',
        dataType:'json',
        success:function(data){
          window.location.reload();
        },
        error: function(r, s) {
          if(r.status == 403)
            alert('Error 403: You are not authorized to perform this action.');
          }
      });
    }
}
function installSortable() {
  var fixHelper = function(e, ui) {
    ui.children().each(function() {
        $(this).width($(this).width());
    });
    return ui;
};
  $('#category-grid table.items tbody').sortable({
    forcePlaceholderSize: true,
    forceHelperSize: true,
    items: 'tr',
    update : function () {
        serial = $('#category-grid table.items tbody').sortable('serialize', {key: 'items[]', attribute: 'class'});
        $.ajax({
            'url': baseUrl + '/admin/category/sort',
            'type': 'post',
            'data': serial,
            'success': function(data){
              $.fn.yiiGridView.update('category-grid');
            },
            'error': function(request, status, error){
                alert('We are unable to set the sort order at this time.  Please try again in a few minutes.');
            }
        });
    },
    helper: fixHelper
  }).disableSelection();
}
$(function() {
  installSortable();
});
</script>